package com.qkl.user.controller;

import com.qkl.user.dto.ExceptionResponse;
import com.qkl.user.enums.ExceptionEnum;
import com.qkl.user.exception.OperationException;
import com.qkl.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private UserService userService;
    /**
     * 单图上传接口
     * @param file 前端上传的图片文件（参数名必须为"file"，或前端对应调整）
     * @return 包含图片访问URL的响应
     */
    @PostMapping("/avatar")
    public ResponseEntity<ExceptionResponse> uploadImage(@RequestParam("file") MultipartFile file) {
        String imageUrl = userService.changeAvatar(file);
        Map<String, String> result = new HashMap<>();
        result.put("images", imageUrl);
        return ResponseEntity.ok(ExceptionResponse.Success(result));
    }
}
